import logger from '../../../../utils/logger';
// Component Object
Component({
  properties: {
    myProperty: {
      type: String,
      value: '',
      observer() {},
    },
    groupidnum:{
      type: String,
      value: '',
      observer(groupidnum) {
        this.setData({
          groupID:groupidnum
        })
      },
    }
  },
  data: {
    groupID: '',
    searchGroup: {},
    lock:false,
    isClick:false
  },
  methods: {
    // 回退
    goBack() {
      if(this.data.groupidnum){
        wx.navigateBack()({
          delta:1
        })
      } else {
        this.triggerEvent('showConversation');
      }
    },
    // 获取输入的群ID
    getGroupIDInput(e) {
      this.setData({
        groupID: e.detail.value,
      });
    },
    // 通过输入的群ID来查找群
    searchGroupByID() {
      console.log(this.data.groupID,'------this.data.groupID')
      wx.$TUIKit.searchGroupByID(this.data.groupID)
        .then((imResponse) => {
          if (imResponse.data.group.groupID !== '') {
            this.setData({
              searchGroup: imResponse.data.group,
            });
          }
        })
        .catch((imError) => {
          console.log(imError)
          wx.hideLoading();
          if (imError.code === 10007) {
            wx.showToast({
              title: '讨论组类型群不允许申请加群',
              icon: 'none',
            });
          } else {
            wx.showToast({
              title: '未找到该群组',
              icon: 'none',
            });
            setTimeout(()=>{
                wx.navigateBack()({
                  delta:1
                })
            },1500)
            //   wx.navigateBack()({
              //     delta:1
              //   })
          }
        });
    },
    // 选择查找到的群
    handleChoose() {
      this.data.searchGroup.isChoose = !this.data.searchGroup.isChoose;
      this.setData({
        searchGroup: this.data.searchGroup,
      });
    },
    // 确认加入
    bindConfirmJoin() {
      if(this.data.lock){
        return false;
      }
      this.setData({
        lock:true
      })
      logger.log(`| TUI-Group | join-group | bindConfirmJoin | groupID: ${this.data.groupID}`);
      wx.$TUIKit.joinGroup({ groupID: this.data.groupID, type: this.data.searchGroup.type })
        .then((imResponse) => {
          if (this.data.searchGroup.isChoose) {
            if (imResponse.data.status === 'WaitAdminApproval') {
              wx.showToast({
                title: '等待管理员同意',
                icon: 'none',
              });
              this.setData({
                lock:false,
                isClick:true
              })

            } else {
              // if(this.data.groupidnum){
              //   wx.navigateBack()({
              //     delta:1
              //   })
              // } else {
                this.triggerEvent('createConversation', { currentConversationID: `GROUP${this.data.searchGroup.groupID}` });
              // }
              this.setData({
                lock:false
              })
            }
          } else {
            wx.showToast({
              title: '请选择相关群聊',
              icon: 'error',
            });
            this.setData({
              lock:false
            })
          }
          switch (imResponse.data.status) {
            case wx.TencentCloudChat.TYPES.JOIN_STATUS_WAIT_APPROVAL:
              // 等待管理员同意
              break;
            case wx.TencentCloudChat.TYPES.JOIN_STATUS_SUCCESS: // 加群成功
              break;
            case wx.TencentCloudChat.TYPES.JOIN_STATUS_ALREADY_IN_GROUP: // 已经在群中
              break;
            default:
              break;
          }
        })
        .catch((imError) => {
          wx.hideLoading();
          if (imError.code === 10007) {
            wx.showToast({
              title: '讨论组类型群不允许申请加群',
              icon: 'none',
            });
          } else {
            wx.showToast({
              title: '未找到该群组',
              icon: 'none',
            });
          }
        });
    },
  },
  created() {

  },
  attached() {

  },
  ready() {
    console.log(this.data.groupID,'---------groupidnum')
    this.searchGroupByID()
  },
  moved() {

  },
  detached() {

  },
});
